//--------------------------------------------------------------------------------
// This file is a portion of the D2D1Renderer.  It is distributed
// under the MIT License, available in the root of this distribution and 
// at the following URL:
//
// http://www.opensource.org/licenses/mit-license.php
//
// Copyright (c) Mirco Timmermann
//
//--------------------------------------------------------------------------------
#include "CheckBox.h"

namespace D2 {

CheckBox::CheckBox(int x, int y, int width, int height) : DisplayObject(x, y, width, height) {
	_highlight = false;
	_toggle = false;
	_pressed = false;

	_text = new Text();
}

CheckBox::~CheckBox(void) {

}

/////////////////////////////DATA////////////////////////////
bool CheckBox::Draw(Direct2DContext* ctx, D2D1::Matrix3x2F* mtx) {
	ctx->lineWidth = 1;
	ctx->SetStrokeStyle(0.3f, 0.3f, 0.3f);
	ctx->SetFillStyle(0.5f, 0.5f, 0.5f);

	ctx->rect(0, 0, _width, _height);
    ctx->fill();
	ctx->stroke();
        
	if(_pressed) {
		ctx->SetFillStyle(0.8f, 0.8f, 0.8f);
		ctx->rect(0, 0, _width, _height);
        ctx->fill();
	} else if(_highlight) {
		ctx->SetFillStyle(0.6f, 0.6f, 0.6f);
		ctx->rect(0, 0, _width, _height);
		ctx->fill();
	}
    
	if(_toggle) {
		ctx->SetStrokeStyle(0.4f, 0.4f, 0.4f);
		ctx->lineWidth = 2;

		float offsetTick = _width*0.2f;

		ctx->beginPath();

		ctx->moveTo(offsetTick, _height*0.5);
		ctx->lineTo(offsetTick + (_width - offsetTick*2)*0.5, offsetTick + _height-offsetTick*2);
		ctx->moveTo(offsetTick + (_width - offsetTick*2)*0.5, offsetTick + _height-offsetTick*2);
		ctx->lineTo(offsetTick + (_width - offsetTick*2), 3);

		ctx->stroke();

		ctx->closePath();
	}

	return true;
}

void CheckBox::OnMouseOver(const MouseEvent &e) {
	_highlight = true;
}

void CheckBox::OnMouseOut(const MouseEvent &e) {
	_highlight = false;
}

void CheckBox::OnMouseDown0(const MouseEvent &e) {
	_pressed = true;
}

void CheckBox::OnMouseUp0(const MouseEvent &e) {
	_pressed = false;
    _toggle = !_toggle;
}

void CheckBox::OnMouseLeave0(const MouseEvent &e) {
	_pressed = false;
}

}